package com.aaa.user.controller;

import com.aaa.entity.UmsMemberReceiveAddress;
import com.aaa.user.service.UserService;
import com.aaa.util.ResultObject;
import com.aaa.util.ResultStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * fileName:UserController
 * description:
 * author:lk
 * createTime:2020/1/9 19:39
 * version:1.0.0
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 根据id获取用户中心的用户信息
     * @param id
     * @return
     */
    @RequestMapping("getUserById")
    public Object getUserById(Long id){
        return userService.getUserById(id);
    }

    /**
     * 根据用户id查询用户收货地址列表
     */
    @RequestMapping("listAddressByMemberId")
    public Object listAddressByMemberId(Long memberId){
        ResultObject resultObjec = new ResultObject<>();
        List<Map> list = userService.listAddressByMemberId(memberId);
        if (list != null && list.size()>0){
            resultObjec.setCode(ResultStatus.SUCCESS.getCode());
            resultObjec.setMsg(ResultStatus.SUCCESS.getMsg());
            resultObjec.setData(list);
        }
        return resultObjec;
    }

    /**
     * 根据id查询收获地址
     * @param id
     * @return
     */
    @RequestMapping("getAddressById")
    public Object getAddressById(Long id){
        return userService.getAddressById(id);
    }

    /**
     * 添加收货地址
     */
    @RequestMapping("addAddress")
    public Object addAddress( @RequestBody() UmsMemberReceiveAddress address ){
        System.out.println(12312312);
        System.out.println(address);
        ResultObject resultObjec = new ResultObject<>();
        int i = userService.addAddress(address);
        if (i>0){
            resultObjec.setCode(ResultStatus.SUCCESS.getCode());
            resultObjec.setMsg(ResultStatus.SUCCESS.getMsg());
            resultObjec.setData(i);
        }
        return resultObjec;
    }

    /**
     * 修改收获地址
     * @param map
     * @return
     */
    @RequestMapping("updateAddress")
    public Object updateAddress( @RequestParam() Map map ){
        System.out.println(map);
        ResultObject resultObjec = new ResultObject<>();
        int i = userService.updateAddress(map);
        if (i>0){
            resultObjec.setCode(ResultStatus.SUCCESS.getCode());
            resultObjec.setMsg(ResultStatus.SUCCESS.getMsg());
            resultObjec.setData(i);
        }
        return resultObjec;
    }

    /**
     * 设置默认收货地址
     */
    @RequestMapping("setDetailAddress")
    public Object setDetailAddress( @RequestParam() Map map ){
        ResultObject resultObjec = new ResultObject<>();
        int i = userService.setDetailAddress(map);
        if (i>0){
            resultObjec.setCode(ResultStatus.SUCCESS.getCode());
            resultObjec.setMsg(ResultStatus.SUCCESS.getMsg());
            resultObjec.setData(i);
        }
        return resultObjec;
    }


    @RequestMapping("deleteAddressById")
    public Object deleteAddressById(Long id ){
        ResultObject resultObjec = new ResultObject<>();
        int i = userService.deleteAddressById(id);
        if (i>0){
            resultObjec.setCode(ResultStatus.SUCCESS.getCode());
            resultObjec.setMsg(ResultStatus.SUCCESS.getMsg());
            resultObjec.setData(i);
        }
        return resultObjec;
    }

    @RequestMapping("payHistory")
    public Object payHistory(Long memberId ){
        ResultObject resultObjec = new ResultObject<>();
        List<Map> list = userService.payHistory(memberId);
        if (list.size()>0 && list != null){
            resultObjec.setCode(ResultStatus.SUCCESS.getCode());
            resultObjec.setMsg(ResultStatus.SUCCESS.getMsg());
            resultObjec.setData(list);
        }
        return resultObjec;
    }
}
